import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTextArea;
//import JXTA libraries
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PeerAdvertisement;

public class PeerFetch extends Thread implements Runnable,DiscoveryListener 
{
    //Class variables
    private DiscoveryService myDiscoveryService=null;
    
    private List<String> listOfPeers;
    public boolean endOfSearch = false;
    private PeerGroup group;
    
    /** Creates a new instance of PeersListing */
    public PeerFetch(PeerGroup group, DiscoveryService myDiscoveryService) 
    {
        this.group = group;
        this.myDiscoveryService = group.getDiscoveryService();
        //System.out.println("gruppe; " + group.getPeerGroupName());
    }
    public void run()
    {  
        while(true)
        {
            if(checkLastTime()){
                break;
            }
            try{
            	myDiscoveryService.addDiscoveryListener(this);
            	myDiscoveryService.getRemoteAdvertisements(// no specific peer (propagate)
    					null, // Adv type
    					DiscoveryService.ADV, // Attribute = any
    					null, // Value = any
    					null, // one advertisement response is all we are looking for
    					1, // no query specific listener. we are using a global listener
    					null);
            Thread.sleep(10*1000);
            
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    private boolean checkLastTime()
    {
        if(endOfSearch == true){
            return true;
        }
        return false;
    }
    public void setEndOfSearch(boolean value) 
    {
        this.endOfSearch = value;
    }
    private void updatePeerList(List<String> myList)
    {
    	listOfPeers = new ArrayList<String>(myList);
    }

    public List<String> getListOfNeighbours() {
    	return listOfPeers;
    }
    
    public void discoveryEvent(DiscoveryEvent event)
    {
        DiscoveryResponseMsg res = event.getResponse();
        String name = "unknown";
        boolean isInList = false;
        PeerAdvertisement peerAdv = res.getPeerAdvertisement();
        if(peerAdv != null){
            name = peerAdv.getName();
        }
        PeerAdvertisement myAdv = null;
        Enumeration en = res.getAdvertisements();
        List<String> peerList = new ArrayList<String>();
       
        if(en != null){
            while(en.hasMoreElements()){
                myAdv = (PeerAdvertisement) en.nextElement();
                peerList.add(myAdv.getName());               
            }           
            updatePeerList(peerList);
        }
    }
    
}
